from ij import IJ, WindowManager,ImagePlus,ImageStack 
from java.awt.event import MouseAdapter
from ij.gui import Roi,PolygonRoi

one=0
one1=1
n=0
n2=0
roiwidth=5
roilength=5
roiperimeter=4*(roilength+roiwidth)
store=[]

store2=[]

def doSomething(imp,canvas):
	global n
	global store
	global roiwidth
	global roilength
	global imp2
	global one
	n2=n
	roiperimeter=4*(roilength+roiwidth)
	if n!=0:		
		oldbp = stack.getProcessor(n)
		for i in range(roiperimeter):
			oldbp.putPixel(store[i],store[i+roiperimeter],store[i+2*roiperimeter])	
	if n==0:
		imp2 = IJ.openImage("C:/Users/admin/Desktop/lab/010640_000000_000000.tif")                           #your own path
		imp2.show()
		win = imp2.getWindow()
		win.getCanvas().addMouseListener(listener2)
	if n2!=0:
		n2=imp2.getCurrentSlice()
		if n2==imp2.getNSlices():
			one=-1
		if n2==1:
			one=1
		imp2.setSlice(n2+one)
	p = canvas.getCursorLoc()
	x = int(p.x)
	y = int(p.y)
	print x,' ',y  
	xarray=range(x-roiwidth,x+roiwidth)+[x+roiwidth for i in range(2*roilength)]+range(x+roiwidth,x-roiwidth,-1)+[x-roiwidth for i in range(2*roilength)]
	yarray=[y-roilength for i in range(2*roiwidth)]+range(y-roilength,y+roilength)+[y+roilength for i in range(2*roiwidth)]+range(y+roilength,y-roilength,-1)
	pixarray=[imp.getPixel(xarray[i],yarray[i])[0] for i in range(roiperimeter)] 
	store=xarray+yarray+pixarray
	ip = imp.getProcessor()
	roi = PolygonRoi([x-roiwidth,x+roiwidth,x+roiwidth,x-roiwidth], [y-roilength,y-roilength,y+roilength,y+roilength], 4, Roi.POLYGON)   
	ip.setValue(255)
	ip.draw(roi)
	imp.updateAndDraw()
	n=imp.getCurrentSlice()


def doSomething2(imp2,canvas):
	global roiwidth
	global roilength
	global store2
	global imp
	global n2
	global one1
	roiperimeter=4*(roilength+roiwidth)
	stack=imp2.getImageStack() 
	if n2!=0:		
		oldbp = stack.getProcessor(n2)
		for i in range(roiperimeter):
			oldbp.putPixel(store2[i],store2[i+roiperimeter],store2[i+2*roiperimeter])	
	n=imp.getCurrentSlice()
	if n==imp.getNSlices():
		one1=-1
	if n==1:
		one1=1
	imp.setSlice(n+one1)
	p = canvas.getCursorLoc()
	x = int(p.x)
	y = int(p.y)
	print x,' ',y  
	xarray=range(x-roiwidth,x+roiwidth)+[x+roiwidth for i in range(2*roilength)]+range(x+roiwidth,x-roiwidth,-1)+[x-roiwidth for i in range(2*roilength)]
	yarray=[y-roilength for i in range(2*roiwidth)]+range(y-roilength,y+roilength)+[y+roilength for i in range(2*roiwidth)]+range(y+roilength,y-roilength,-1)
	pixarray=[imp.getPixel(xarray[i],yarray[i])[0] for i in range(roiperimeter)] 
	store2=xarray+yarray+pixarray
	ip = imp2.getProcessor()
	roi = PolygonRoi([x-roiwidth,x+roiwidth,x+roiwidth,x-roiwidth], [y-roilength,y-roilength,y+roilength,y+roilength], 4, Roi.POLYGON)   
	ip.setValue(255)
	ip.draw(roi)
	imp2.updateAndDraw()
	n2=imp2.getCurrentSlice()

	
class ML(MouseAdapter):
	def mousePressed(self, event):
		canvas = event.getSource()
		imp = canvas.getImage()
		doSomething(imp,canvas)

class ML2(MouseAdapter):
	def mousePressed(self, event):
		canvas = event.getSource()
		imp2 = canvas.getImage()
		doSomething2(imp2,canvas)
		

imp2=[]
imp = IJ.getImage() 
stack = imp.getImageStack() 
listener = ML()
listener2 = ML2()
IJ.runPlugIn("ij.plugin.Zoom","scale")
IJ.runPlugIn("ij.plugin.frame.ContrastAdjuster","")

for imp in map(WindowManager.getImage, WindowManager.getIDList()):
	win = imp.getWindow()
	if win is None:
		continue
	win.getCanvas().addMouseListener(listener)


